package modelo2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author User all
 */
public class Conexao {
    
    private String host;
    private String user;
    private String pass;
    private String database;
    public String sql;
    boolean isConnected = false;
    
    public Connection c;
    
    /**
     * Construtor da classe
     * 
     * @param host Servidor que se deseja conectar 
     * @param database Nome do banco que se deseja conectar
     * @param user Nome do usuario
     * @param pass Senha do usuario
     */
    
    public Conexao( String host, String database, String user, String pass) {
        
        this.pass = pass;
        this.user = user;
        this.host = host;
        this.database = database;
    }

    public ResultSet executaQuery(String sql) throws ClassNotFoundException, 
            InstantiationException, IllegalAccessException, SQLException{
        
        String url = "jdbc:mysql://"+this.host+"/"
              +this.database+"?"
              +"user="+this.user
              +"&password="+this.pass;              
       
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            System.out.println(url);
            this.c = DriverManager.getConnection(url);
            this.isConnected = true;
           
            Statement stm = c.createStatement();
            
            ResultSet r = stm.executeQuery(sql);
            //c.close();
            return r;
            
    }
    public void executaUpdate(String sql) throws ClassNotFoundException, 
            InstantiationException, IllegalAccessException, SQLException{
        
        String url = "jdbc:mysql://"+this.host+"/"
              +this.database+"?"
              +"user="+this.user
              +"&password="+this.pass;              
       
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            System.out.println(url);
            this.c = DriverManager.getConnection(url);
            this.isConnected = true;
           
            Statement stm = c.createStatement();
            
            stm.executeUpdate(sql);
            //c.close();
           
    }
    
}